home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / system-config-printer / troubleshoot / CheckPrinterSanity.py < prev    next >
Text File  |  2009-10-19  |  5KB  |  131 lines

  1. #!/usr/bin/env python
  2.  
  3. ## Printing troubleshooter
  4.  
  5. ## Copyright (C) 2008, 2009 Red Hat, Inc.
  6. ## Copyright (C) 2008, 2009 Tim Waugh <twaugh@redhat.com>
  7.  
  8. ## This program is free software; you can redistribute it and/or modify
  9. ## it under the terms of the GNU General Public License as published by
  10. ## the Free Software Foundation; either version 2 of the License, or
  11. ## (at your option) any later version.
  12.  
  13. ## This program is distributed in the hope that it will be useful,
  14. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. ## GNU General Public License for more details.
  17.  
  18. ## You should have received a copy of the GNU General Public License
  19. ## along with this program; if not, write to the Free Software
  20. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. import cups
  23. import gobject
  24. import os
  25. import smburi
  26. import subprocess
  27. from timedops import TimedOperation, TimedSubprocess
  28. import urllib
  29. from base import *
  30. class CheckPrinterSanity(Question):
  31.     def __init__ (self, troubleshooter):
  32.         Question.__init__ (self, troubleshooter, "Check printer sanity")
  33.         troubleshooter.new_page (gtk.Label (), self)
  34.         self.troubleshooter = troubleshooter
  35.  
  36.     def display (self):
  37.         # Collect information useful for the various checks.
  38.  
  39.         self.answers = {}
  40.  
  41.         answers = self.troubleshooter.answers
  42.         if not answers['cups_queue_listed']:
  43.             return False
  44.  
  45.         name = answers['cups_queue']
  46.  
  47.         parent = self.troubleshooter.get_window ()
  48.  
  49.         # Find out if this is a printer or a class.
  50.         try:
  51.             cups.setServer ('')
  52.             c = TimedOperation (cups.Connection, parent=parent).run ()
  53.             printers = TimedOperation (c.getPrinters, parent=parent).run ()
  54.             if printers.has_key (name):
  55.                 self.answers['is_cups_class'] = False
  56.                 queue = printers[name]
  57.                 self.answers['cups_printer_dict'] = queue
  58.             else:
  59.                 self.answers['is_cups_class'] = True
  60.                 classes = TimedOperation (c.getClasses, parent=parent).run ()
  61.                 queue = classes[name]
  62.                 self.answers['cups_class_dict'] = queue
  63.  
  64.             attrs = TimedOperation (c.getPrinterAttributes, (name,),
  65.                                     parent=parent).run ()
  66.             self.answers['local_cups_queue_attributes'] = attrs
  67.         except:
  68.             pass
  69.  
  70.         if self.answers.has_key ('cups_printer_dict'):
  71.             cups_printer_dict = self.answers['cups_printer_dict']
  72.             uri = cups_printer_dict['device-uri']
  73.             (scheme, rest) = urllib.splittype (uri)
  74.             self.answers['cups_device_uri_scheme'] = scheme
  75.             if scheme in ["ipp", "http", "https"]:
  76.                 (hostport, rest) = urllib.splithost (rest)
  77.                 (host, port) = urllib.splitnport (hostport, defport=631)
  78.                 self.answers['remote_server_name'] = host
  79.                 self.answers['remote_server_port'] = port
  80.             elif scheme == "smb":
  81.                 u = smburi.SMBURI (uri)
  82.                 (group, host, share, user, password) = u.separate ()
  83.                 os.environ['HOST'] = host
  84.                 if group:
  85.                     os.environ['GROUP'] = group
  86.                     cmdline = 'LC_ALL=C nmblookup -W "$GROUP" "$HOST"'
  87.                 else:
  88.                     cmdline = 'LC_ALL=C nmblookup "$HOST"'
  89.                 try:
  90.                     p = TimedSubprocess (parent=parent,
  91.                                          timeout=5000,
  92.                                          args=cmdline, shell=True,
  93.                                          stdin=file("/dev/null"),
  94.                                          stdout=subprocess.PIPE,
  95.                                          stderr=subprocess.PIPE)
  96.                     result = p.run ()
  97.                     self.answers['nmblookup_output'] = result
  98.                     for line in result[0]:
  99.                         if line.startswith ("querying"):
  100.                             continue
  101.                         spc = line.find (' ')
  102.                         if (spc != -1 and
  103.                             not line[spc:].startswith (" failed ")):
  104.                             # Remember the IP address.
  105.                             self.answers['remote_server_name'] = line[:spc]
  106.                             break
  107.                 except OSError:
  108.                     # Problem executing command.
  109.                     pass
  110.             elif scheme == "hp":
  111.                 os.environ['URI'] = uri
  112.                 try:
  113.                     p = TimedSubprocess (parent=parent,
  114.                                          timeout=3000,
  115.                                          args='LC_ALL=C DISPLAY= hp-info -d"$URI"',
  116.                                          shell=True,
  117.                                          stdin=file("/dev/null"),
  118.                                          stdout=subprocess.PIPE,
  119.                                          stderr=subprocess.PIPE)
  120.                     self.answers['hplip_output'] = p.run ()
  121.                 except OSError:
  122.                     # Problem executing command.
  123.                     pass
  124.  
  125.             r = cups_printer_dict['printer-type'] & cups.CUPS_PRINTER_REMOTE
  126.             self.answers['cups_printer_remote'] = (r != 0)
  127.         return False
  128.  
  129.     def collect_answer (self):
  130.         return self.answers
  131.